/**
 * 
 */
package the_Crash;

import java.util.LinkedList;
import java.util.List;

import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;


/**
 * @author Hariharan
 *
 */
//creates a street which will in turn create the streetCells comprising the street
public class Street  {
	
	private Integer x;
	
	private Integer y;
	
	private Grid<Object> grid;
	
	private ContinuousSpace<Object> space;
	
	private LinkedList<StreetCell> cells;

	public Street(Integer x, Integer y, Grid<Object> grid,
			ContinuousSpace<Object> space) {
		super();
		this.x = x;
		this.y = y;
		this.grid = grid;
		this.space = space;
		createCells();
	}
	
	//**************************************************************************
	
	public List<StreetCell> GetCells() {
		return cells;
	}
	
	//*************************************************************************************
	
	private void createCells() {
		cells = new LinkedList<StreetCell>();

		if (x == null) { // Horizontal
			for (int i = 0; i < grid.getDimensions().getWidth() ; i++) {
					for (int j = -CityBuilder.lanesOnSide ; j <=  CityBuilder.lanesOnSide ; j++) {
						boolean canTurnRight = false;
						if(Math.abs(j)==CityBuilder.lanesOnSide)
							canTurnRight=true;
						StreetCell cell = null;
						if(j<0)
							cell = new StreetCell(new GridPoint(i ,y+j),canTurnRight, false, true, Direction.East, grid, space);
						else if(j==0)
						{
							if(i%(ParameterResolver.getBlockSize()+CityBuilder.lanesPerStreet)>=CityBuilder.lanesPerStreet -CityBuilder.lanesOnSide && i%(ParameterResolver.getBlockSize()+CityBuilder.lanesPerStreet)<CityBuilder.lanesPerStreet+CityBuilder.turnLaneLength)
								cell = new StreetCell(new GridPoint(i ,y+j),false, true, true, Direction.West, grid, space);
							if(i%(ParameterResolver.getBlockSize()+CityBuilder.lanesPerStreet)>=CityBuilder.lanesPerStreet+ParameterResolver.getBlockSize()-CityBuilder.turnLaneLength || i%(ParameterResolver.getBlockSize()+CityBuilder.lanesPerStreet)<CityBuilder.lanesOnSide)
								cell = new StreetCell(new GridPoint(i ,y+j),false, true, true, Direction.East, grid, space);
						}
						else if(j>0)
							cell = new StreetCell(new GridPoint(i ,y+j),canTurnRight, false, true, Direction.West, grid, space);
						if(cell!=null)
							cells.add(cell);
				}
			}
			
		} else { // Vertical
			for (int i = 0; i < grid.getDimensions().getWidth() ; i++) {
				for (int j = -CityBuilder.lanesOnSide ; j <=  CityBuilder.lanesOnSide ; j++) {
					boolean canTurnRight = false;
					if(Math.abs(j)==CityBuilder.lanesOnSide)
						canTurnRight=true;
					StreetCell cell = null;
						if(j<0)
							cell = new StreetCell(new GridPoint(x+j,i),canTurnRight, false, true, Direction.South, grid, space);
						else if(j==0)
						{
							if(i%(ParameterResolver.getBlockSize()+CityBuilder.lanesPerStreet)>=CityBuilder.lanesPerStreet -CityBuilder.lanesOnSide && i%(ParameterResolver.getBlockSize()+CityBuilder.lanesPerStreet)<CityBuilder.lanesPerStreet+CityBuilder.turnLaneLength)
								cell = new StreetCell(new GridPoint(x+j,i),false, true, true, Direction.South, grid, space);
							if(i%(ParameterResolver.getBlockSize()+CityBuilder.lanesPerStreet)>=CityBuilder.lanesPerStreet+ParameterResolver.getBlockSize()-CityBuilder.turnLaneLength || i%(ParameterResolver.getBlockSize()+CityBuilder.lanesPerStreet)<CityBuilder.lanesOnSide)
								cell = new StreetCell(new GridPoint(x+j,i),false, true, true, Direction.North, grid, space);
						}
						else if(j>0)
							cell = new StreetCell(new GridPoint(x+j,i),canTurnRight, false, true, Direction.North, grid, space);
					if(cell!=null)
						cells.add(cell);
				}
			}
		}
	}
}
